package finalVersion;

import java.awt.BorderLayout;


import javax.swing.JFrame;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;
import javax.swing.JTextArea;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.io.PrintWriter;
import java.net.Socket;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;


public class ChatGUI extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private JPanel contentPane;
	private JTextField chatField;
	private JTextArea messageArea;

	private Socket chatDirectSocket = null;
	private PrintWriter out = null;
	private BufferedReader in = null;
	private String User;

	/**
	 * Create the frame.
	 */
	public ChatGUI() {
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnApps = new JMenu("Apps");
		menuBar.add(mnApps);

		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
				
			}
		});
		mnApps.add(mntmExit);

		JMenu mnAbout = new JMenu("About");
		menuBar.add(mnAbout);

		JMenuItem mntmAboutChat = new JMenuItem("About Chat");
		mnAbout.add(mntmAboutChat);
		contentPane = new JPanel();
		contentPane.setBorder(new TitledBorder(null, "Message",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.SOUTH);
		panel_1.setLayout(new MigLayout("", "[][grow]", "[]"));

		JLabel lblChat = new JLabel("Chat");
		panel_1.add(lblChat, "cell 0 0,alignx trailing");

		chatField = new JTextField();
		panel_1.add(chatField, "flowx,cell 1 0,growx");
		chatField.setColumns(10);
		chatField.addActionListener(this);

		JButton btnSend = new JButton("Send");
		panel_1.add(btnSend, "cell 1 0");
		btnSend.addActionListener(this);

		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);

		messageArea = new JTextArea();
		messageArea.setEditable(false);
		scrollPane.setViewportView(messageArea);
	}
	
	public void setID(String user) {
		this.User = user;
	}

	public void setSocket(Socket s) {
		this.chatDirectSocket = s;	
	}
	
	public void setDetailConnection(String addr, int port) {
		try {
			chatDirectSocket = new Socket(addr,port);
		} catch (Exception e) {
			System.out.println(e);
		}
	}

	
	
	public void run() {
		try {
			out = new PrintWriter(chatDirectSocket.getOutputStream());
			in = new BufferedReader(new InputStreamReader(chatDirectSocket
					.getInputStream()));
			this.sendMessage(User);
			setTitle(in.readLine() + " - " + "Private Message");
			Thread read = new Thread(new RemoteReader());
			read.start();
			setVisible(true);
		} catch (Exception e) {
			System.out.println(e);
		}
	}

	private class RemoteReader implements Runnable {
		public void run() {
			String receive;
			try {
				while ((receive= in.readLine()) != null) {
					if(receive.startsWith("Disconnect")) {
//						sendMessage("Disconnect");
//						exit();
						
					}
					else {
						setVisible(true);
						messageArea.append(receive + "\n");
					}
				}

			} catch (IOException e) {
				System.out.println(e);
			}
		}
	}

	private void exit() {
		try {		
			chatDirectSocket.close();			
		} catch (Exception e) {
			System.err
					.println("Error while shutting down the GUI.");
		}
		System.exit(0);
	}

	public void sendMessage(String s) {
		out.println(s);
		out.flush();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String chat = chatField.getText();
		messageArea.append(User + "::" + chat + "\n");
		chatField.setText("");
		out.println(User + "::" + chat);
		out.flush();
	}

}
